<template>
	<view class="admin-dashboard">
		<!-- 统计卡片 -->
		<view class="stats-section">
			<view class="stat-card">
				<view class="stat-icon users">👥</view>
				<view class="stat-info">
					<text class="stat-number">{{ stats.totalUsers || 0 }}</text>
					<text class="stat-label">总用户数</text>
				</view>
			</view>
			
			<view class="stat-card">
				<view class="stat-icon orders">📋</view>
				<view class="stat-info">
					<text class="stat-number">{{ stats.totalOrders || 0 }}</text>
					<text class="stat-label">总订单数</text>
				</view>
			</view>
			
			<view class="stat-card">
				<view class="stat-icon revenue">💰</view>
				<view class="stat-info">
					<text class="stat-number">￥{{ stats.totalRevenue || 0 }}</text>
					<text class="stat-label">总交易额</text>
				</view>
			</view>
			
			<view class="stat-card">
				<view class="stat-icon techs">🔧</view>
				<view class="stat-info">
					<text class="stat-number">{{ stats.activeTechs || 0 }}</text>
					<text class="stat-label">活跃技术员</text>
				</view>
			</view>
		</view>
		
		<!-- 快捷操作 -->
		<view class="quick-actions">
			<view class="section-title">管理功能</view>
			<view class="action-grid">
				<view class="action-item" @click="goUserManagement">
					<view class="action-icon user-mgmt">👤</view>
					<text class="action-text">用户管理</text>
				</view>
				
				<view class="action-item" @click="goOrderManagement">
					<view class="action-icon order-mgmt">📊</view>
					<text class="action-text">订单管理</text>
				</view>
				
				<view class="action-item" @click="goCategoryManagement">
					<view class="action-icon category-mgmt">🏷️</view>
					<text class="action-text">分类管理</text>
				</view>
				
				<view class="action-item" @click="goSystemSettings">
					<view class="action-icon settings">⚙️</view>
					<text class="action-text">系统设置</text>
				</view>
			</view>
		</view>
		
		<!-- 最新订单 -->
		<view class="recent-orders">
			<view class="section-title">
				<text>最新订单</text>
				<text class="more" @click="goOrderManagement">查看全部</text>
			</view>
			<view class="order-list">
				<view class="order-item" v-for="order in recentOrders" :key="order.id" @click="goOrderDetail(order.id)">
					<view class="order-header">
						<text class="order-title">{{ order.title }}</text>
						<text class="order-status" :class="getStatusClass(order.status)">
							{{ getStatusText(order.status) }}
						</text>
					</view>
					<view class="order-info">
						<text class="budget">￥{{ order.budget }}</text>
						<text class="user">{{ order.userName }}</text>
						<text class="time">{{ formatTime(order.createTime) }}</text>
					</view>
				</view>
				
				<view class="empty" v-if="recentOrders.length === 0">
					<text>暂无订单数据</text>
				</view>
			</view>
		</view>
		
		<!-- 用户统计 -->
		<view class="user-stats">
			<view class="section-title">用户统计</view>
			<view class="stats-chart">
				<view class="chart-item">
					<view class="chart-bar">
						<view class="bar-fill admin" :style="{ height: getBarHeight(userStats.admins) }"></view>
					</view>
					<text class="chart-label">管理员</text>
					<text class="chart-value">{{ userStats.admins || 0 }}</text>
				</view>
				
				<view class="chart-item">
					<view class="chart-bar">
						<view class="bar-fill dispatcher" :style="{ height: getBarHeight(userStats.dispatchers) }"></view>
					</view>
					<text class="chart-label">派单员</text>
					<text class="chart-value">{{ userStats.dispatchers || 0 }}</text>
				</view>
				
				<view class="chart-item">
					<view class="chart-bar">
						<view class="bar-fill user" :style="{ height: getBarHeight(userStats.users) }"></view>
					</view>
					<text class="chart-label">用户</text>
					<text class="chart-value">{{ userStats.users || 0 }}</text>
				</view>
				
				<view class="chart-item">
					<view class="chart-bar">
						<view class="bar-fill tech" :style="{ height: getBarHeight(userStats.techs) }"></view>
					</view>
					<text class="chart-label">技术员</text>
					<text class="chart-value">{{ userStats.techs || 0 }}</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				stats: {},
				recentOrders: [],
				userStats: {},
				loading: false
			}
		},
		onLoad() {
			this.checkPermission()
			this.loadDashboardData()
		},
		methods: {
			checkPermission() {
				if (!this.$store.getters.isAdmin) {
					uni.showToast({
						title: '权限不足',
						icon: 'none'
					})
					setTimeout(() => {
						uni.navigateBack()
					}, 1500)
					return false
				}
				return true
			},
			
			async loadDashboardData() {
				this.loading = true
				try {
					// 加载统计数据
					await Promise.all([
						this.loadStats(),
						this.loadRecentOrders(),
						this.loadUserStats()
					])
				} catch (error) {
					uni.showToast({
						title: '加载数据失败',
						icon: 'none'
					})
				} finally {
					this.loading = false
				}
			},
			
			async loadStats() {
				try {
					const res = await this.$api.getAdminStats()
					if (res.code === 200) {
						this.stats = res.data || {}
					}
				} catch (error) {
					uni.showToast({
						title: '加载统计数据失败',
						icon: 'none'
					})
				}
			},
			
			async loadRecentOrders() {
				try {
					const res = await this.$api.getOrderList({ pageSize: 5 })
					if (res.code === 200) {
						this.recentOrders = res.data || []
					}
				} catch (error) {
					uni.showToast({
						title: '加载订单数据失败',
						icon: 'none'
					})
				}
			},
			
			async loadUserStats() {
				try {
					const res = await this.$api.getUserStats()
					if (res.code === 200) {
						this.userStats = res.data || {}
					}
				} catch (error) {
					uni.showToast({
						title: '加载用户统计失败',
						icon: 'none'
					})
				}
			},
			
			goUserManagement() {
				uni.navigateTo({
					url: '/pages/admin/users'
				})
			},
			
			goOrderManagement() {
				uni.navigateTo({
					url: '/pages/admin/orders'
				})
			},
			
			goCategoryManagement() {
				uni.showToast({
					title: '功能开发中',
					icon: 'none'
				})
			},
			
			goSystemSettings() {
				uni.showToast({
					title: '功能开发中',
					icon: 'none'
				})
			},
			
			goOrderDetail(id) {
				uni.navigateTo({
					url: `/pages/order/detail?id=${id}`
				})
			},
			
			getBarHeight(value) {
				if (!value || value === 0) return '5%'
				const max = Math.max(this.userStats.admins || 0, this.userStats.dispatchers || 0, this.userStats.users || 0, this.userStats.techs || 0) || 1
				const height = Math.max((value / max * 95), 5)
				return height + '%'
			},
			
			getStatusClass(status) {
				const statusMap = {
					1: 'status-pending',
					2: 'status-processing',
					3: 'status-reviewing',
					4: 'status-completed',
					5: 'status-cancelled'
				}
				return statusMap[status] || ''
			},
			
			getStatusText(status) {
				const statusMap = {
					1: '待接单',
					2: '进行中',
					3: '待验收',
					4: '已完成',
					5: '已取消'
				}
				return statusMap[status] || '未知'
			},
			
			formatTime(time) {
				if (!time) return ''
				const date = new Date(time)
				const now = new Date()
				const diff = now - date
				
				if (diff < 60000) return '刚刚'
				if (diff < 3600000) return Math.floor(diff / 60000) + '分钟前'
				if (diff < 86400000) return Math.floor(diff / 3600000) + '小时前'
				return Math.floor(diff / 86400000) + '天前'
			}
		}
	}
</script>

<style lang="scss" scoped>
	.admin-dashboard {
		background: #f5f5f5;
		min-height: 100vh;
		padding: 20rpx;
	}
	
	.stats-section {
		display: flex;
		flex-wrap: wrap;
		gap: 20rpx;
		margin-bottom: 40rpx;
		
		.stat-card {
			flex: 1;
			min-width: 300rpx;
			max-width: calc(50% - 10rpx);
		}
		
		@media (max-width: 750rpx) {
			.stat-card {
				min-width: 100%;
				max-width: 100%;
			}
		}
		
		.stat-card {
			flex: 1;
			min-width: 300rpx;
			background: #fff;
			border-radius: 15rpx;
			padding: 30rpx;
			display: flex;
			align-items: center;
			box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
			
			.stat-icon {
				width: 80rpx;
				height: 80rpx;
				border-radius: 40rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				margin-right: 30rpx;
				font-size: 36rpx;
				
				&.users {
					background: linear-gradient(45deg, #667eea, #764ba2);
				}
				
				&.orders {
					background: linear-gradient(45deg, #4ECDC4, #44A08D);
				}
				
				&.revenue {
					background: linear-gradient(45deg, #FFD89B, #19547B);
				}
				
				&.techs {
					background: linear-gradient(45deg, #FF6B6B, #FF8E53);
				}
			}
			
			.stat-info {
				flex: 1;
				
				.stat-number {
					display: block;
					font-size: 36rpx;
					font-weight: bold;
					color: #333;
					margin-bottom: 10rpx;
				}
				
				.stat-label {
					font-size: 24rpx;
					color: #666;
				}
			}
		}
	}
	
	.section-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 30rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		
		.more {
			font-size: 24rpx;
			color: #007AFF;
			font-weight: normal;
		}
	}
	
	.quick-actions {
		margin-bottom: 40rpx;
		
		.action-grid {
			display: flex;
			flex-wrap: wrap;
			gap: 20rpx;
			
			.action-item {
				flex: 1;
				min-width: 160rpx;
				background: #fff;
				border-radius: 15rpx;
				padding: 40rpx 20rpx;
				text-align: center;
				box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
				
				.action-icon {
					width: 80rpx;
					height: 80rpx;
					border-radius: 40rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					margin: 0 auto 20rpx;
					font-size: 36rpx;
					
					&.user-mgmt {
						background: linear-gradient(45deg, #667eea, #764ba2);
					}
					
					&.order-mgmt {
						background: linear-gradient(45deg, #4ECDC4, #44A08D);
					}
					
					&.category-mgmt {
						background: linear-gradient(45deg, #FFD89B, #19547B);
					}
					
					&.settings {
						background: linear-gradient(45deg, #FF6B6B, #FF8E53);
					}
				}
				
				.action-text {
					font-size: 24rpx;
					color: #666;
				}
			}
		}
	}
	
	.recent-orders {
		margin-bottom: 40rpx;
		
		.order-list {
			.order-item {
				background: #fff;
				border-radius: 15rpx;
				padding: 30rpx;
				margin-bottom: 20rpx;
				box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
				
				.order-header {
					display: flex;
					justify-content: space-between;
					align-items: center;
					margin-bottom: 20rpx;
					
					.order-title {
						flex: 1;
						font-size: 28rpx;
						font-weight: bold;
						color: #333;
						margin-right: 20rpx;
					}
					
					.order-status {
						font-size: 24rpx;
						padding: 4rpx 12rpx;
						border-radius: 12rpx;
					}
				}
				
				.order-info {
					display: flex;
					justify-content: space-between;
					align-items: center;
					
					.budget {
						font-size: 28rpx;
						font-weight: bold;
						color: #FF6B6B;
					}
					
					.user {
						font-size: 24rpx;
						color: #666;
					}
					
					.time {
						font-size: 24rpx;
						color: #999;
					}
				}
			}
			
			.empty {
				text-align: center;
				padding: 80rpx 0;
				color: #999;
				font-size: 28rpx;
			}
		}
	}
	
	.user-stats {
		.stats-chart {
			background: #fff;
			border-radius: 15rpx;
			padding: 40rpx;
			box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
			display: flex;
			justify-content: space-around;
			align-items: flex-end;
			height: 300rpx;
			
			.chart-item {
				text-align: center;
				display: flex;
				flex-direction: column;
				align-items: center;
				
				.chart-bar {
					width: 60rpx;
					height: 200rpx;
					background: #f0f0f0;
					border-radius: 30rpx;
					position: relative;
					margin-bottom: 20rpx;
					
					.bar-fill {
						position: absolute;
						bottom: 0;
						width: 100%;
						border-radius: 30rpx;
						transition: height 0.3s ease;
						
						&.admin {
							background: linear-gradient(to top, #667eea, #764ba2);
						}
						
						&.dispatcher {
							background: linear-gradient(to top, #4ECDC4, #44A08D);
						}
						
						&.user {
							background: linear-gradient(to top, #FFD89B, #19547B);
						}
						
						&.tech {
							background: linear-gradient(to top, #FF6B6B, #FF8E53);
						}
					}
				}
				
				.chart-label {
					font-size: 24rpx;
					color: #666;
					margin-bottom: 10rpx;
				}
				
				.chart-value {
					font-size: 28rpx;
					font-weight: bold;
					color: #333;
				}
			}
		}
	}
</style>